package com.jt.service;

import com.jt.mapper.ItemCatMapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemCat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.logging.Level;

@Service
public class ItemCatServiceImpl implements ItemCatService{

    @Autowired
    private ItemCatMapper itemCatMapper;

    public Map<Integer,List<ItemCat>> getMap(){
        Map<Integer,List<ItemCat>> map = new HashMap<>();
        //1.查询所有的数据库信息
        List<ItemCat> list = itemCatMapper.findItemCarList();
        //2.遍历数据封装Map
        for (ItemCat itemCat : list){
            int key = itemCat.getParentId();
            if (map.containsKey(key)){
                map.get(key).add(itemCat);
            }else {//key不存在，则为第一个数据
                List<ItemCat> tempList = new ArrayList<>();
                tempList.add(itemCat);
                map.put(key,tempList);
            }
        }
        return map;
    }

    @Override
    public List<ItemCat> findItemCatList(Integer level) {
         Map<Integer,List<ItemCat>> map = getMap();

        if (level == 1)
            return map.get(0);

        if (level == 2)
            return getTwoList(map);

        return getThreeList(map);
    }

    @Override
    @Transactional
    public void saveItemCat(ItemCat itemCat) {
        Date date = new Date();
        itemCat.setStatus(true).setCreated(date).setUpdated(date);
        itemCatMapper.saveItemCat(itemCat);
    }

    @Override
    @Transactional
    public void updateStatus(ItemCat itemCat) {
        itemCat.setUpdated(new Date());
        itemCatMapper.updateStatus(itemCat);

    }

    @Override
    public void updateItemCat(ItemCat itemCat) {
        itemCat.setUpdated(new Date());
        itemCatMapper.updateItemCat(itemCat);
    }

    @Override
    public void deleteItemCat(ItemCat itemCat) {
        if (itemCat.getLevel() == 3)
            itemCatMapper.deleteItemCat3(itemCat.getId());
        if (itemCat.getLevel() == 2)
            itemCatMapper.deleteItemCat2(itemCat.getId());
        if (itemCat.getLevel() == 1)
            itemCatMapper.deleteItemCat1(itemCat.getId());

    }

    private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> oneList = getTwoList(map);
        for (ItemCat oneItemCat : oneList){
            List<ItemCat> twoList = oneItemCat.getChildren();
            if (twoList == null || twoList.size() == 0)
                continue;
            for (ItemCat twoItemCat : twoList){
                Integer twoId = twoItemCat.getId();
                List<ItemCat> threeList = map.get(twoId);
                twoItemCat.setChildren(threeList);
            }
        }
        return oneList;
    }

    private List<ItemCat> getTwoList(Map<Integer,List<ItemCat>> map) {
        List<ItemCat> oneList = map.get(0);
        for (ItemCat oneItemCat : oneList){
            Integer id = oneItemCat.getId();
            List<ItemCat> twoList = map.get(id);
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }


    /**
     * 连接数据库是消耗性能的主要矛盾点
     * */
//    @Override
//    public List<ItemCat> findItemCatList(Integer level) {
//        //1.查询一级商品分类信息
//        int parentId = 0;
//        List<ItemCat> oneList = itemCatMapper.findItemCatByParentId(parentId);
//
//        //2.查询二级商品分类信息
//        for (ItemCat ontItemCat: oneList){
//            int oneId = ontItemCat.getId();
//            List<ItemCat> twoList = itemCatMapper.findItemCatByParentId(oneId);
//            ontItemCat.setChildren(twoList);
//            for (ItemCat twoItemCat : twoList){
//                int twoId = twoItemCat.getId();
//                List<ItemCat> threeList = itemCatMapper.findItemCatByParentId(twoId);
//                twoItemCat.setChildren(threeList);
//            }
//        }
//        return oneList;
//    }
}
